package vue;

import javax.swing.JPanel;
import javax.swing.border.EmptyBorder;
import javax.swing.JTable;
import javax.swing.JButton;
import javax.swing.JLabel;

import modele.Commande;
import org.jdesktop.beansbinding.BeanProperty;

import java.util.GregorianCalendar;
import java.util.List;
import modele.LigneCommande;
import modele.util.StatutCommande;

import org.jdesktop.swingbinding.JTableBinding;
import org.jdesktop.swingbinding.SwingBindings;
import org.jdesktop.beansbinding.AutoBinding.UpdateStrategy;
import org.jdesktop.beansbinding.ELProperty;

import controleur.PasserCommandeController;

import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import java.awt.event.ComponentAdapter;
import java.awt.event.ComponentEvent;

public class Recapitulatif extends JPanel {
	private JTable table;
	private Commande commande;
	private PasserCommandeController cmdctrl;
	private JButton button;

	public PasserCommandeController getCmdctrl() {
		return cmdctrl;
	}

	public void setCmdctrl(PasserCommandeController cmdctrl) {
		this.cmdctrl = cmdctrl;
	}

	public Commande getCommande() {
		return commande;
	}

	public void setCommande(Commande commande) {
		this.commande = commande;
		initDataBindings();
	}

	/**
	 * Create the panel.
	 */
	public Recapitulatif() {
		addComponentListener(new ComponentAdapter() {
			@Override
			public void componentShown(ComponentEvent arg0) {
				table.repaint();
			}
		});
		setLayout(null);
		
		JPanel panel = new JPanel();
		panel.setLayout(null);
		panel.setBorder(new EmptyBorder(5, 5, 5, 5));
		panel.setBounds(16, 11, 434, 262);
		add(panel);
		
		table = new JTable();
		table.setBounds(10, 11, 424, 155);
		panel.add(table);
		
		button = new JButton("Valider la commande");
		button.setBounds(130, 196, 141, 23);
		button.addActionListener(new ActionListener() {
			
			@Override
			public void actionPerformed(ActionEvent arg0) {
				// TODO Auto-generated method stub
				commande.setDateCommande((new GregorianCalendar()).getTime());
				commande.setStatut(StatutCommande.ENCOURS);
				cmdctrl.storeCommande(commande);
				
			}
		});
		panel.add(button);
		
		JLabel label = new JLabel("Total :");
		label.setBounds(260, 171, 46, 14);
		panel.add(label);
		
		JLabel label_1 = new JLabel("");
		label_1.setBounds(332, 171, 46, 14);
		panel.add(label_1);
		initDataBindings();

	}
	protected void initDataBindings() {
		if (commande==null)
			System.out.println("ok");
		
		if (commande!=null)
			System.out.println("ok+");
		
		BeanProperty<Commande, List<LigneCommande>> commandeBeanProperty = BeanProperty.create("lignesDeCommandes");
		JTableBinding<LigneCommande, Commande, JTable> jTableBinding = SwingBindings.createJTableBinding(UpdateStrategy.READ, commande, commandeBeanProperty, table);
		//
		BeanProperty<LigneCommande, String> ligneCommandeBeanProperty = BeanProperty.create("produit.nom");
		jTableBinding.addColumnBinding(ligneCommandeBeanProperty).setColumnName("New Column").setEditable(false);
		//
		BeanProperty<LigneCommande, Double> ligneCommandeBeanProperty_1 = BeanProperty.create("produit.prix");
		jTableBinding.addColumnBinding(ligneCommandeBeanProperty_1).setColumnName("Prix unitaire").setEditable(false);
		//
		BeanProperty<LigneCommande, Integer> ligneCommandeBeanProperty_2 = BeanProperty.create("quantite");
		jTableBinding.addColumnBinding(ligneCommandeBeanProperty_2).setColumnName("Quantit\u00E9").setEditable(false);
		//
		ELProperty<LigneCommande, Object> ligneCommandeEvalutionProperty = ELProperty.create("${produit.prix*quantite}");
		jTableBinding.addColumnBinding(ligneCommandeEvalutionProperty).setColumnName("Total").setEditable(false);
		//
		jTableBinding.bind();
	}
}
